/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is iRecipe code.
 *
 * The Initial Developer of the Original Code is Robert Morris.
 * Portions created by the Initial Developer are Copyright (C) 2006
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *
 * ***** END LICENSE BLOCK ***** */

package com.pavisoft.irecipe;

import java.util.ArrayList;

/**
 * @author Robert Morris
 *
 * Holds a collection of <code>Recipe</code>s. Useful for creating categories,
 * such as "Breads" or "Appetizers".
 *
 */
public class RecipeGroup implements Cloneable
{
    private String name;
    private String comments;
    private ArrayList<Recipe> recipes = new ArrayList<Recipe>();
    
    /** Creates a new <code>RecipeGroup</code> with the specified name. */
    public RecipeGroup(String name)
    {
        this.name = name;
    }
    
    /** Returns whether or not this <code>RecipeGroup</code> is empty (i.e., has
     *  zero elements).
     *  @return <code>true</code> if this <code>RecipeGroup</code> contains one
     *   or more recipes; else <code>false</code>. */
    public boolean isEmpty()
    {
        return this.recipes.isEmpty();
    }
    
    /** Returns the name of this recipe. */
    public String getName()
    {
        return this.name;
    }
    
    /** Sets the name of this recipe to the given string. */
    public void setName(String name)
    {
        this.name = name;
    }
    
    /** Returns the comments/description field of this recipe. */
    public String getComments()
    {
        return this.comments;
    }
    
    /** Sets the comments/description field of this recipe to the given value. */
    public void setComments(String comments)
    {
        this.comments = comments;
    }
    
    /** Adds the given recipe to this <code>RecipeGroup</code>.
     *  @param recipe The <code>Recipe</code> to add */
    public void addRecipe(Recipe recipe)
    {
        this.recipes.add(recipe);
    }
    
    /** Adds the given recipe to this <code>RecipeGroup</code> at the given position. (If
     *  <code>index</code> is greater than the length of the recipes collection, the
     *  recipe will simply be added to the end of the collection.)
     *  @param recipe The <code>Recipe</code to add
     *  @param index The index at which to add this recipe */
    public void addRecipe(Recipe recipe, int index)
    {
        if (index >= recipes.size())
            recipes.add(recipe);
        else
            recipes.add(index, recipe);
    }

    /** Removes the recipe at the given index from this <code>RecipeGroup</code>.
     * @param recipeIndex The index of the recipe to remove */
    public void removeRecipe(int recipeIdx)
    {
        this.recipes.remove(recipeIdx);
    }
    
    /** Removes the given <code>Recipe</code> from this <code>RecipeGroup</code>. */
    public void removeRecipe(Recipe recipe)
    {
        this.recipes.remove(recipe);
    }
    
    /** Clears the <code>Recipe</code>s contained in this <code>RecipeGroup</code> and
     *  replaces them with the given list of recipes.
     *  @param recipes An <code>ArrayList<Recipe></code> containing the
     *  <code>Recipe</code>s to put in this <code>RecipeGroup</code> (erasing any other
     *  recipes, if present) */
    public void setRecipes(ArrayList<Recipe> recipes)
    {
        this.recipes = recipes;
    }
    
    /** Returns an array of all the <code>Recipe</code>s contained in this
     *  <code>RecipeGroup</code>. */
    public Recipe[] getRecipes()
    {
        return this.recipes.toArray(new Recipe[recipes.size()]);
    }
    
    /** Returns the <code>Recipe</code> located at the given <code>index</code>. */
    public Recipe getRecipe(int index)
    {
        return this.recipes.get(index);
    }
    
    /** Makes a semi-shallow clone of this <code>RecipeGroup</code>; does not
     *  clone the <code>Recipe</code>s within. (For the Cocoa-Java version of iRecipe
     *  on OS X, it seems implementation of this method is required for this class.
     *  Use of this method by consumers of this class is not recommended.)
     */
    public Object clone()
    {
        RecipeGroup rg = new RecipeGroup(this.name);
        rg.setComments(this.comments);
        rg.setRecipes(new ArrayList<Recipe>(this.recipes));
        return rg;
    } 
    
    /** Moves the recipe at <code>fromIndex</code> to <code>toIndex</code>.
     *  (Convienent for drag-and-drop GUIs.)
     *  @param fromIndex The index of the recipe to move
     *  @param toIndex The index to which to move the recipe. (Note that this
     *   paramater reflects the index before modification.)
     */
    public void moveRecipe(int fromIndex, int toIndex)
    {
        if (fromIndex == toIndex) return;
        
        this.recipes.add(toIndex, this.recipes.get(fromIndex));
        
        if (fromIndex > toIndex)
        {
            this.recipes.remove(fromIndex + 1);
        }
        else
        {
            this.recipes.remove(fromIndex);
        }
    }
}

